//
// Created by chezi008 on 2020/1/15.
//

#include "device.h"
#include "Log.h"
#include "utils.h"

void Device::GetDevInfo() {
    LOGD("GetDevInfo->username:%s,onvif_addr:%s，%p", username.data(), onvif_addr.data(), &onvifmsg);
    string result = onvifmsg.GetDevInfo(username, pwd, onvif_addr);
    vector<string> vec_devinfo;
    splitString(result, "$", vec_devinfo);
    if (vec_devinfo.size()>1){
        dev_name.append(vec_devinfo[0]);
        dev_type.append(vec_devinfo[1]);
        LOGD("GetDevInfo->dev_name:%s", dev_name.data());
        LOGD("GetDevInfo->dev_type:%s", dev_type.data());
    } else{
        dev_name.append("empty");
        dev_type.append("empty");
    }
}

void Device::GetOnvifServeAddr() {
    string result = onvifmsg.GetOnvifServeAddr(username, pwd, onvif_addr);

    splitString(result, "$", vec_service);
    LOGD("GetOnvifServeAddr->result:%s", result.data());
    LOGD("GetOnvifServeAddr->media:%s", vec_service[SERVICE_INDEX_MEDIA].data());
    for (int i = 0; i < vec_service.size(); i++)//size()容器中实际数据个数
    {
        if (vec_service[i] == "empty") {
            continue;
        }
        //判断摄像机返回的ip和init的ip是否相同
        vector<string> strDest;
        splitString(vec_service[i], "/", strDest);
        int begin = vec_service[i].find(":");
        if (strDest[2] != ip) {
//        LOGD("ip:%s",strDest[1].data());
            vec_service[i].replace(begin + 3, strDest[1].length(), ip);
        }
        LOGD("uri:%s", vec_service[i].data());
    }
    LOGD("GetOnvifMediaUri->SERVICE_INDEX_MEDIA:%s", vec_service[SERVICE_INDEX_MEDIA].data());
}

void Device::GetOnvifMediaUri() {
    LOGD("GetOnvifMediaUri->mediarul:%s", vec_service[SERVICE_INDEX_MEDIA].data());
    onvifmsg.GetOnvifMediaUri(username, pwd, onvif_addr, vec_service[SERVICE_INDEX_MEDIA], dev_type,
                              map_passmsg);
    //token 默认选择第一个
    source_token = map_passmsg.begin()->first;
}

void Device::SetTokenByUri(string uri) {
    //通过uri 找到对应的token
    auto ite = map_passmsg.begin();
    for (ite; ite != map_passmsg.end(); ite++) {
        if (uri == ite->second.uri) {
            source_token = ite->first;
            LOGD("SetTokenByUri-->find sourcetoken:%s", source_token.data());
            break;
        }
    }
}

int Device::DoPtz(int ptz_type, int ptz_command, float ptz_speed) {
    if (source_token.empty()) {
        return -1;
    }
    PassMsg passMsg = map_passmsg[source_token];
    int result = onvifmsg.DoPtz(username, pwd, onvif_addr, vec_service[SERVICE_INDEX_PTZ],
                                passMsg.token,
                                passMsg.type, ptz_type, ptz_command, ptz_speed);
    return 0;
}

int Device::PtzStop() {
    if (source_token.empty()) {
        return -1;
    }
    PassMsg passMsg = map_passmsg[source_token];
    int result = onvifmsg.DoPtz(username, pwd, onvif_addr, vec_service[SERVICE_INDEX_PTZ],
                                passMsg.token, passMsg.type,
                                5, 12, 0);
    return 0;
}